#include <iostream>
#include <windows.h>
#include <string>
#include <thread>
#include <chrono>
#include <map>
#include <cwchar>

// 增强版键码映射（兼容非美式键盘布局）
std::map<wchar_t, std::pair<BYTE, bool>> keymap = {
    {L'a', {0x41, false}}, {L'A', {0x41, true}},
    {L'b', {0x42, false}}, {L'B', {0x42, true}},
    {L'c', {0x43, false}}, {L'C', {0x43, true}},
    {L'd', {0x44, false}}, {L'D', {0x44, true}},
    {L'e', {0x45, false}}, {L'E', {0x45, true}},
    {L'f', {0x46, false}}, {L'F', {0x46, true}},
    {L'g', {0x47, false}}, {L'G', {0x47, true}},
    {L'h', {0x48, false}}, {L'H', {0x48, true}},
    {L'i', {0x49, false}}, {L'I', {0x49, true}},
    {L'j', {0x4A, false}}, {L'J', {0x4A, true}},
    {L'k', {0x4B, false}}, {L'K', {0x4B, true}},
    {L'l', {0x4C, false}}, {L'L', {0x4C, true}},
    {L'm', {0x4D, false}}, {L'M', {0x4D, true}},
    {L'n', {0x4E, false}}, {L'N', {0x4E, true}},
    {L'o', {0x4F, false}}, {L'O', {0x4F, true}},
    {L'p', {0x50, false}}, {L'P', {0x50, true}},
    {L'q', {0x51, false}}, {L'Q', {0x51, true}},
    {L'r', {0x52, false}}, {L'R', {0x52, true}},
    {L's', {0x53, false}}, {L'S', {0x53, true}},
    {L't', {0x54, false}}, {L'T', {0x54, true}},
    {L'u', {0x55, false}}, {L'U', {0x55, true}},
    {L'v', {0x56, false}}, {L'V', {0x56, true}},
    {L'w', {0x57, false}}, {L'W', {0x57, true}},
    {L'x', {0x58, false}}, {L'X', {0x58, true}},
    {L'y', {0x59, false}}, {L'Y', {0x59, true}},
    {L'z', {0x5A, false}}, {L'Z', {0x5A, true}},
    {L'0', {0x30, false}}, {L')', {0x30, true}},
    {L'1', {0x31, false}}, {L'!', {0x31, true}},
    {L'2', {0x32, false}}, {L'@', {0x32, true}},
    {L'3', {0x33, false}}, {L'#', {0x33, true}},
    {L'4', {0x34, false}}, {L'$', {0x34, true}},
    {L'5', {0x35, false}}, {L'%', {0x35, true}},
    {L'6', {0x36, false}}, {L'^', {0x36, true}},
    {L'7', {0x37, false}}, {L'&', {0x37, true}},
    {L'8', {0x38, false}}, {L'*', {0x38, true}},
    {L'9', {0x39, false}}, {L'(', {0x39, true}},
    {L' ', {VK_SPACE, false}},
    {L'\n', {VK_RETURN, false}},
    {L'\t', {VK_TAB, false}},
    {L'-', {0xBD, false}}, {L'_', {0xBD, true}},
    {L'=', {0xBB, false}}, {L'+', {0xBB, true}},
    {L'[', {0xDB, false}}, {L'{', {0xDB, true}},
    {L']', {0xDD, false}}, {L'}', {0xDD, true}},
    {L'\\', {0xDC, false}}, {L'|', {0xDC, true}},
    {L';', {0xBA, false}}, {L':', {0xBA, true}},
    {L'\'', {0xDE, false}}, {L'"', {0xDE, true}},
    {L',', {0xBC, false}}, {L'<', {0xBC, true}},
    {L'.', {0xBE, false}}, {L'>', {0xBE, true}},
    {L'/', {0xBF, false}}, {L'?', {0xBF, true}}
};

// 改进的驱动级输入（解决Shift状态冲突）
void SendHardwareKey(BYTE vk, bool shift) {
    // 主输入序列
    INPUT inputs[4] = {};
    int count = 0;

    if (shift) {
        inputs[count].type = INPUT_KEYBOARD;
        inputs[count].ki.wVk = VK_SHIFT;
        count++;
    }
    inputs[count].type = INPUT_KEYBOARD;
    inputs[count].ki.wVk = vk;
    count++;
    inputs[count].type = INPUT_KEYBOARD;
    inputs[count].ki.wVk = vk;
    inputs[count].ki.dwFlags = KEYEVENTF_KEYUP;
    count++;
    if (shift) {
        inputs[count].type = INPUT_KEYBOARD;
        inputs[count].ki.wVk = VK_SHIFT;
        inputs[count].ki.dwFlags = KEYEVENTF_KEYUP;
        count++;
    }
    for(int i=0; i<count; i++){
        INPUT current[1] = {inputs[i]};
        SendInput(1, current, sizeof(INPUT));
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
}
// 增强的Hyper-V输入函数
void TypeInHyperV(const std::wstring& text) {
    for (wchar_t c : text) {
        auto it = keymap.find(c);
        if (it != keymap.end()) {
            auto [vk, shift] = it->second;
            SendHardwareKey(vk, shift);
        }
    }
}

// 获取剪贴板文本（Unicode版）
std::wstring GetClipboardTextUnicode() {
    if (!OpenClipboard(NULL)) return L"";

    HANDLE hData = GetClipboardData(CF_UNICODETEXT);
    if (!hData) {
        CloseClipboard();
        return L"";
    }

    wchar_t* pszText = static_cast<wchar_t*>(GlobalLock(hData));
    if (!pszText) {
        CloseClipboard();
        return L"";
    }

    std::wstring text(pszText);
    GlobalUnlock(hData);
    CloseClipboard();
    return text;
}

int main() {
    // 使用Windows API确保正确输出中文
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    if (hConsole != INVALID_HANDLE_VALUE) {
        const wchar_t* msg = L"Hyper-V专用输入工具 (Ctrl+Alt+V开始输入)\n";
        DWORD written;
        WriteConsoleW(hConsole, msg, wcslen(msg), &written, NULL);
    }
    while (true) {
        // 修正热键检测逻辑（检查高位和低位）
        if ((GetAsyncKeyState(VK_CONTROL) & 0x8000) != 0 &&
            (GetAsyncKeyState(VK_MENU) & 0x8000) != 0 &&
            (GetAsyncKeyState(0x56) & 0x8000) != 0) {  // V键

            std::this_thread::sleep_for(std::chrono::milliseconds(500));
            std::wstring text = GetClipboardTextUnicode();

            if (!text.empty()) {
                // 修复点：动态计算长度
                std::wstring msg = L"正在输入到Hyper-V\n";
                DWORD written;
                WriteConsoleW(hConsole, msg.c_str(), static_cast<DWORD>(msg.length()), &written, NULL);

                TypeInHyperV(text);
            }
        }
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }
}
